package com.zhdl.common.service;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.zhdl.common.constant.Dic;
import com.zhdl.common.interceptor.CacheInfo;
import com.zhdl.common.interceptor.MyAssert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static com.zhdl.common.constant.HttpCodeEnum.UNAUTHORIZED;

/**
 * 访问拦截器业务处理
 */
@Slf4j
@Service
public class RequestService {

    @Autowired
    private TokenService tokenService;

    /**
     * 存放用户登录的 token 信息
     */
    private TimedCache<String, Map<String, String>> TOKEN_CACHE = CacheUtil.newTimedCache(72000000);


    //验证token
    public boolean checkToken(String token) {

        // 执行认证
        MyAssert.isBlank(token, UNAUTHORIZED.getCode(), "token is null");
        log.info(token + "-----------------------------");
        try {
            // 获取 token 中的 openId
            String userId = tokenService.getUserId(token);
            log.info(userId);
            // 验证 token
            tokenService.verifier(userId).verify(token);
        } catch (JWTVerificationException e) {
            if (log.isErrorEnabled()) {
                MyAssert.fail(UNAUTHORIZED.getCode(), e, "非法 token");
            }
        }
        return true;
    }

    //设置缓存信息
    public void setCacheUser(String userId) throws IOException {

        //  String userId ="u001"; //tokenService.getUserId(token);
        //设置用户缓存基本信息
//        String userName="test";
//        String orgId="000";
//        String orgName="测试院校";
//
        Map<String, String> map = new HashMap<>(6);
        map.put("userId", userId);
//        map.put("userName", userName);
//        map.put("orgId", orgId);
//        map.put("orgName", orgName);
//        map.put("areaId", "a001");
//        String userKey = getUserKey(userId);

        if (!TOKEN_CACHE.containsKey(userId)) {
//            redisService.hmset(userKey,map,Dic.CACHE_TIME);
            TOKEN_CACHE.put(userId, map, Dic.CACHE_TIME);
        }
        TOKEN_CACHE.put(userId, map, Dic.CACHE_TIME);
    }

    //获得缓存信息
    public CacheInfo tokenCacheUser(String userId) {
//        String userId ="u001";
        return getCacheInfo(userId);
    }

    public CacheInfo getCacheInfo(String userId) {
        String userKey = getUserKey(userId);
        CacheInfo u = new CacheInfo();
        Map<String, String> map = TOKEN_CACHE.get(userKey);
        u.setUserId(map.get("userId"));
//        u.setUserName(map.get("userName").toString());
//        u.setOrgId(map.get("orgId").toString());
//        u.setOrgName(map.get("orgName").toString());
//        u.setAreaId(map.get("areaId").toString());
        return u;
    }


    private String getUserKey(String userId) {
        return Dic.CACHE_LOGIN_USER + userId;
    }
}
